// Strip.cpp: implementation of the CStrip class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Strip.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStrip::CStrip()
{
	mHeight = 0;
	mBin = -1;
	mIsBacktracked = false;
}

CStrip::CStrip(float height)
{
	mHeight = height;
	mBin = -1;
	mIsBacktracked = false;
}


CStrip::~CStrip()
{

}

////////////////CStripList implementation////////////.

CStripList::CStripList()
{
	mListStrip = NULL;
	mCurrentAvailableStrip = -1;
	mSize = 0;
}

CStripList::CStripList(float *listHeight, int stripNumber)
{
	mSize = stripNumber;
	mListStrip = new CStrip[stripNumber];
	for (int i = 0; i < mSize; i++)
	{
		mListStrip[i].SetHeight(listHeight[i]);
	}
	mCurrentAvailableStrip = 0;
}

CStripList::CStripList(CStripList &stripList, int stripNumber, bool copyFilledStrip)
{
	mSize = stripNumber;
	mListStrip = new CStrip[mSize];
	int startIndex = 0;
	if (!copyFilledStrip)
		startIndex = stripList.GetCurrentAvailableStrip();
	for (int i = 0; i < mSize; i++)
	{
		mListStrip[i].SetHeight(stripList.GetValue(startIndex + i).GetHeight());
		mListStrip[i].SetBin(stripList.GetValue(startIndex + i).GetBin());
	}
	if (!copyFilledStrip)
		mCurrentAvailableStrip = 0;
	else
		mCurrentAvailableStrip = stripList.GetCurrentAvailableStrip();
}
CStripList::CStripList(CStripList &stripList)
{
	//CStripList(stripList, stripList.GetStripNumber(), true);
	mSize = stripList.GetSize();
	mListStrip = new CStrip[mSize];
	for (int i = 0; i < mSize; i++)
	{
		mListStrip[i].SetHeight(stripList.GetValue(i).GetHeight());
		mListStrip[i].SetBin(stripList.GetValue(i).GetBin());
	}
	mCurrentAvailableStrip = stripList.mCurrentAvailableStrip;
}
CStripList::~CStripList()
{
	if (mListStrip !=NULL)
		delete [] mListStrip;
}

CStrip* CStripList::GetLongestAvailableStrip()
{
	if (mCurrentAvailableStrip<0 || mCurrentAvailableStrip> mSize-1)
		return NULL;
	else
		return mListStrip + mCurrentAvailableStrip;
}


bool CStripList::SetBinValue(int index, int binIndex)
{
	//if (mListStrip[index].GetBin()!=-1)
		//return false;
	//When we set a bin to the strip, we move it to the beginning of the array, and move the current
	float currentHeight = mListStrip[index].GetHeight();
	for (int i = mCurrentAvailableStrip; i< index; i++)
		mListStrip[i + 1].SetHeight(mListStrip[i].GetHeight());
	mListStrip[mCurrentAvailableStrip].SetHeight(currentHeight);
	mListStrip[mCurrentAvailableStrip].SetBin(binIndex);
	mCurrentAvailableStrip++;
	return true;
}
bool CStripList::SetBinValueForUnfilledElm(int index, int binIndex)
{ 
	return SetBinValue(index+mCurrentAvailableStrip, binIndex);
}
void CStripList::ResetBinValue(int index)
{
	float currentHeight = mListStrip[index].GetHeight();
	int i;

	if (index < mCurrentAvailableStrip - 1)
	{
		for (int i = index+1; i<mCurrentAvailableStrip; i++)
			mListStrip[i-1].SetHeight(mListStrip[i].GetHeight());
	}
	for (i = mCurrentAvailableStrip; i< mSize; i++)
		if (mListStrip[i].GetHeight() <= currentHeight)
			break;
	for (int j = mCurrentAvailableStrip; j < i; j++)
	{
		mListStrip[j-1].SetHeight(mListStrip[j].GetHeight());
		mListStrip[j-1].SetBin(mListStrip[j].GetBin());
	}
	mListStrip[i-1].SetBin(-1);
	mListStrip[i-1].SetHeight(currentHeight);
	mCurrentAvailableStrip--;
}